<?php
session_start();
require_once('config/config.php');
require_once('fpdf/tfpdf.php');

/* connessione al db */
$mysqli = new mysqli($_CFG[connect][host], $_CFG[connect][user], $_CFG[connect][pass], $_CFG[connect][database]);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//setta iddoc passato da $_GET
if(isset($_GET[id])){
	$iddoc = $_GET[id];
	$_CFG[printer][pdf_autoprint] = false; //disattiva la stampa automatica 
}else{
	require_once('cartsave.inc.php');
}

//recupera i dati dal database
$sql_h = 'SELECT * FROM `documenti` WHERE `IDdocumento` = '.$iddoc.' AND `IDdocstato` != '.$_CFG[stati][eliminato];
if ($result = $mysqli->query($sql_h)) {
	$rhead = $result->fetch_array(MYSQLI_BOTH);
	//$rhead[] => `IDdocumento`, `sessione`, `numero`, `dataora`, `tavolo`, `nome`, `username`, `IDdocstato`
	$numerodoc = $rhead[numero];
	$numerotav = ($rhead[tavolo] == '')? '_____': $rhead[tavolo];
	$clientetav = $rhead[nome];
	$dataora = $rhead[dataora];
	list($data_en,$ora) = explode(' ',$dataora);
	list($YY,$MM,$DD) = explode('-',$data_en);
	$data = "$DD/$MM/$YY";
	$result->free();
}
//recupera i dati di quella riga
//*
#$sql_r = 'SELECT `descrizione`, `qta`, (`qta`*`prezzo`) AS tot FROM `docrighe` WHERE `IDdocumento` = '.$iddoc;
$sql_r = 'SELECT *, (`qta`*`prezzo`) AS tot FROM `docrighe` WHERE `IDdocumento` = '.$iddoc.' ORDER BY IDcategoria, descrizione';
if ($result = $mysqli->query($sql_r)) {
	unset($righe);
	while ($row = $result->fetch_array(MYSQLI_BOTH)) //MYSQLI_NUM))
	{
		$righe[] = $row;
	}
}
#*/
// classe per la stampa immediata
class PDF_JavaScript extends tFPDF {

    var $javascript;
    var $n_js;

    function IncludeJS($script) {
        $this->javascript=$script;
    }

    function _putjavascript() {
        $this->_newobj();
        $this->n_js=$this->n;
        $this->_out('<<');
        $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
        $this->_out('>>');
        $this->_out('endobj');
        $this->_newobj();
        $this->_out('<<');
        $this->_out('/S /JavaScript');
        $this->_out('/JS '.$this->_textstring($this->javascript));
        $this->_out('>>');
        $this->_out('endobj');
    }

    function _putresources() {
        parent::_putresources();
        if (!empty($this->javascript)) {
            $this->_putjavascript();
        }
    }

    function _putcatalog() {
        parent::_putcatalog();
        if (!empty($this->javascript)) {
            $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
        }
    }
}

class PDF_AutoPrint extends PDF_JavaScript
{
	function AutoPrint($dialog=false)
	{
		//Open the print dialog or start printing immediately on the standard printer
		$param=($dialog ? 'true' : 'false');
		if($dialog){
			$script="print($param);";
			$this->IncludeJS($script);
		}
	}

	function AutoPrintToPrinter($server, $printer, $dialog=false)
	{
		//Print on a shared printer (requires at least Acrobat 6)
		$script = "var pp = getPrintParams();";
		if($dialog)
			$script .= "pp.interactive = pp.constants.interactionLevel.full;";
		else
			$script .= "pp.interactive = pp.constants.interactionLevel.automatic;";
		$script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
		$script .= "print(pp);";
		$this->IncludeJS($script);
	}
}
//carico il template di stampa
if(@file_exists($_CFG[printer][template_url].$_CFG[printer][tempalte_name].'.php') ) {
       include($_CFG[printer][template_url].$_CFG[printer][tempalte_name].'.php');
} else {
	$pdf=new PDF_AutoPrint('P', 'mm', array(99,99));//'L','mm',array(100,150)
	$pdf->SetMargins(5,5);
	$pdf->SetAutoPageBreak(5);
	$pdf->AddPage();
	$pdf->SetFont('Arial','',12);
	$pdf->Cell(89,89,'Template non caricato',1,1,'C');
}

	/* azzera la sessione */
	unset($_SESSION['cart']);
	$_SESSION['cart'][$_CFG[pos][default_coperto]]=1;
	unset($_SESSION['cart-tavolo']);
	unset($_SESSION['cart-cliente']);
	//$_SESSION['cart-tavolo']=$_CFG[pos][default_tavolo];
	//$_SESSION['cart-cliente']=$_CFG[pos][default_name];
	#*/
	
//Open the print dialog
$pdf->AutoPrint($_CFG[printer][pdf_autoprint]);	
header("Content-Type: application/pdf");
$pdf->Output();
